<template>
	<div>
        <van-nav-bar
			title="添加地址"
			left-text="返回"
			left-arrow
			@click-left="this.$router.go(-1)" />
		<van-address-edit
			:area-list="areaList"
			show-set-default
			:search-result="searchResult"
			@save="onSave" />

        <!-- 网络请求加载状态 -->
		<van-overlay :show="isLoading" style="display: flex; justify-content: center; align-items: center;">
			<van-loading vertical>
				<template #icon>
					<van-icon name="star-o" size="30" />
				</template>
				加载中...
			</van-loading>
		</van-overlay>
	</div>
</template>

<script>
    import { areaList } from "@vant/area-data";
    import { showFailToast, showSuccessToast } from 'vant';
	export default {
        data() {
            return {
                areaList,
                isLoading: false
            }
        },
        methods:{
            async onSave(val){
                this.isLoading = true;
                let data = {
                    busid: this.$cookies.get("User").id,
                    consignee: val.name,
                    areaCode: val.areaCode,
                    address: val.addressDetail,
                    status: val.isDefault? 1: 0,
                    mobile: val.tel
                }
                let res = await this.$api.AddressAdd(data);
                if (res.code == 1) {
                    showSuccessToast({
                        message: res.msg
                    }); 
                    this.$router.go(-1);
                } else {
                    showFailToast({
                        message: res.msg
                    });
                }
                this.isLoading = false;
            }
        }
    };
</script>

<style></style>
